package edu.vital.energy.longevity.ch10.controller;

import edu.vital.energy.longevity.ch10.domain.Comment;
import edu.vital.energy.longevity.ch10.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    // 新增方法：根据 t_article 的 ID 查询评论
    @GetMapping("/article/{articleId}")
    public String findCommentsByArticleId(@PathVariable("articleId") int articleId, Model model) {
        List<Comment> comments = commentService.findCommentsByArticleId(articleId);
        model.addAttribute("comments", comments);
        return "articleComments"; // 返回新的 HTML 页面
    }

    @GetMapping("/get/{id}")
    public Comment findById(@PathVariable("id") int comment_id){
        Comment comment = commentService.findById(comment_id);
        return comment;
    }

    @GetMapping("/{articleId}")
    public String showCommentsPage(@PathVariable("articleId") int articleId,Model model) {
        List<Comment> comments = commentService.findAllComments(articleId);
        model.addAttribute("comments", comments);
        return "comment";
    }


    // 修正1: 为addComment添加PostMapping注解

    @PostMapping("/add/{articleId}")
    // 添加评论
    public String addComment(@PathVariable("articleId") int articleId, @ModelAttribute Comment comment) {
        // 设置评论的文章ID
        comment.setAId(articleId);
        // 插入评论
        commentService.insertComment(comment);
        // 返回文章评论页面
        return  "redirect:/comments/article/" + articleId;
    }

    // 新增方法：编辑评论页面
    @GetMapping("/edit/{id}")
    public String editComment(@PathVariable("id") int id, Model model) {
        Comment comment = commentService.findCommentById(id);
        model.addAttribute("comment", comment);
        return "editComment"; // 返回编辑页面
    }

    // 修改评论
    @PostMapping("/update/{id}/{aId}")
    public String updateComment(@PathVariable("id") int id,@PathVariable("aId") int aId, @ModelAttribute Comment comment, Model model) {
        comment.setId(id);
        comment.setAId(aId);
        List<Comment> ret = commentService.updateComment(comment);
        model.addAttribute("comments", ret);
        return "articleComments";
    }

    // 修正2: 确保保留的deleteComment方法使用正确路径
    @GetMapping("/delete/{id}")
    public String deleteComment(@PathVariable("id") int id) {
        Comment comment = commentService.findCommentById(id);
        int articleId = comment.getAId();
        commentService.deleteComment(id);
        return "redirect:/comments/article/" + articleId;
    }
//    @ModelAttribute 用于绑定 HTML 表单提交的 application/x-www-form-urlencoded 数据，适用于普通表单提交。
//    @RequestBody 仅适用于接收 JSON 或 XML 格式的请求体，通常用于 AJAX 请求或 API 调用。
//    如果你希望保留 @RequestBody，则需在前端使用 AJAX 提交 JSON 格式数据，并在请求头中设置 Content-Type: application/json
}

